package com.goddess.yeecolor.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.goddess.yeecolor.Modle.Comment;
import com.goddess.yeecolor.Modle.basic;
import com.goddess.yeecolor.Modle.user_info;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.interfaceAll.LoadMoreDataListener;
import com.goddess.yeecolor.interfaceAll.OnClickListener;
import com.goddess.yeecolor.tools.ImageUtils;
import com.goddess.yeecolor.tools.LogUtils;
import com.goddess.yeecolor.view.CircleImageView;
import com.goddess.yeecolor.view.LoadingDialog;
import com.goddess.yeecolor.view.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论主题
 */
public class Item_Exerciseitem_commitAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private boolean isLoading;
    private int totalItemCount;
    private int lastVisibleItemPosition;
    //当前滚动的position下面最小的items的临界值
    private int visibleThreshold = 5;



    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    public static final int NULL_MORE = 1;
    //上拉加载更多状态-默认为0
    private int load_more_status = 0;


    private final int FOOTERTYPE = 1;
    private final int NULL = 0;
    private OnClickListener onClickListener;
    private LayoutInflater inflater;
    private SimpleAdapterViewHolder viewHolderAdapter;
    private Context context;
    private View.OnClickListener replyToCommentListener;
    private View.OnClickListener replyToReplyListener;
    private List<Comment> commentList;
    private Activity mActivity;
    private boolean isshow;//删除是否显示

    public Item_Exerciseitem_commitAdapter2(Context context, Activity activity, List<Comment> commentList,
                                            View.OnClickListener replyToCommentListener,
                                            item_Exerciseitem_commit_contentAdapter myAdapter, View.OnClickListener replyToReplyListener, boolean isshow, int load_more_status) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.commentList = new ArrayList<Comment>();
        this.commentList.addAll(commentList);
        this.replyToCommentListener = replyToCommentListener;
        this.replyToReplyListener = replyToReplyListener;
        this.isshow = isshow;
        this.mActivity = activity;
        this.load_more_status = load_more_status;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == FOOTERTYPE) {
            View view = LayoutInflater.from(context).inflate(R.layout.footer, parent, false);
            FootAdaterViewHolder viewHolder = new FootAdaterViewHolder(view);
            return viewHolder;
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_comment, parent, false);
            SimpleAdapterViewHolder viewHolder = new SimpleAdapterViewHolder(view, true);
            return viewHolder;
        }
    }

    public boolean isIsshow;

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return FOOTERTYPE;
        } else {
            return NULL;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FootAdaterViewHolder) {
            FootAdaterViewHolder foothold = (FootAdaterViewHolder) holder;
            switch (load_more_status) {
                case PULLUP_LOAD_MORE:
                    foothold.progressBar.setVisibility(View.VISIBLE);
                    foothold.footer_text.setText("上拉加载更多...");
                    break;
                case NULL_MORE:
                    foothold.progressBar.setVisibility(View.GONE);
                    foothold.footer_text.setText("已经全部加载完毕");
                    break;
                case 2:
                    foothold.progressBar.setVisibility(View.GONE);
                    foothold.footer_text.setText("暂无数据");
                    break;
            }
        } else {
            Comment comment = commentList.get(position);
            user_info user_info = comment.getInfo();
            basic basic = user_info.getBasic();
            ImageUtils.getInstance().ImageStart(context, basic.getThumb(), ((SimpleAdapterViewHolder) holder).iv_user_photo);
            ((SimpleAdapterViewHolder) holder).tv_user_name.setText(basic.getNickname());
            ((SimpleAdapterViewHolder) holder).tv_user_comment.setText(comment.getContent());
            // 设置评论列表的点击效果透明
            ((SimpleAdapterViewHolder) holder).lv_user_comment_replys.setSelector(new ColorDrawable(
                    Color.TRANSPARENT));
            ((SimpleAdapterViewHolder) holder).lv_user_comment_replys2.setSelector(new ColorDrawable(
                    Color.TRANSPARENT));
            ((SimpleAdapterViewHolder) holder).tv_user_time.setText(comment.getTime());
            ((SimpleAdapterViewHolder) holder).tv_user_praise.setText(comment.getZan() + "");
            ((SimpleAdapterViewHolder) holder).tv_user_praise.setTag(position);
            ((SimpleAdapterViewHolder) holder).btn_comment_reply.setText(comment.getComment());
            LogUtils.getInstence().info(comment.getContent());
            /**
             * 点赞
             */

            ((SimpleAdapterViewHolder) holder).tv_user_praise.setOnClickListener((view) -> {
                /**
                 * 执行网络请求  更新UI  通过接口实现类似Item单行点击
                 */
                int tag = holder.getLayoutPosition();
                onClickListener.OnClick(view, tag);
            });

            if (isshow) {//判断是否有删除按钮
                ((SimpleAdapterViewHolder) holder).tv_user_comment_detele.setVisibility(View.VISIBLE);
                ((SimpleAdapterViewHolder) holder).lv_user_comment_replys2.setVisibility(View.VISIBLE);
                /**
                 * 删除
                 */
                ((SimpleAdapterViewHolder) holder).tv_user_comment_detele.setOnClickListener((view) -> {
                    LoadingDialog dialog = new LoadingDialog(context, mActivity, "确认删除", 2);
                    dialog.setCanceledOnTouchOutside(true);

                    dialog.show();
                    dialog.call.setText("确认");
                    dialog.call.setOnClickListener((view1) -> {
                        dialog.dismiss();
                        commentList.clear();
                        notifyDataSetChanged();
                    });

                });
                //判断当前评论是否有回复
                if (commentList.get(position).getCommentses() != null && commentList.get(position).getCommentses().size() != 0) {

                    ((SimpleAdapterViewHolder) holder).lv_user_comment_replys2
                            .setAdapter(new item_Exerciseitem_commit_contentAdapter(context, commentList
                                    .get(position).getCommentses(), position,
                                    replyToReplyListener, isshow));
                }
                //记录点击回复按钮时对应的position,用于确定所回复的对象
                ((SimpleAdapterViewHolder) holder).btn_comment_reply.setTag(position);
                ((SimpleAdapterViewHolder) holder).btn_comment_reply.setOnClickListener(replyToCommentListener);

            } else {
                ((SimpleAdapterViewHolder) holder).lv_user_comment_replys2.setVisibility(View.GONE);
                //判断当前评论是否有回复
                if (commentList.get(position).getCommentses() != null && commentList.get(position).getCommentses().size() != 0) {
                    ((SimpleAdapterViewHolder) holder).lv_user_comment_replys
                            .setAdapter(new item_Exerciseitem_commit_contentAdapter(context, commentList
                                    .get(position).getCommentses(), position,
                                    replyToReplyListener, isshow));
                }
                //记录点击回复按钮时对应的position,用于确定所回复的对象
                ((SimpleAdapterViewHolder) holder).btn_comment_reply.setTag(position);
                ((SimpleAdapterViewHolder) holder).btn_comment_reply.setOnClickListener(replyToCommentListener);
            }
        }

    }


    @Override
    public int getItemCount() {
        return commentList.size() + 1;
    }

    public class SimpleAdapterViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView iv_user_photo; // 评论者 头像
        private TextView tv_user_name; // 评论者 昵称
        private TextView tv_user_comment; // 评论者 一级品论内容
        private LinearLayout commit_linear;
        private TextView btn_comment_reply; // 评论者 二级评论按钮
        private MyListView lv_user_comment_replys, lv_user_comment_replys2; // 评论者 二级品论内容列表
        private TextView tv_user_praise;
        private TextView tv_user_time, tv_user_comment_detele;

        public SimpleAdapterViewHolder(View itemView, boolean b) {
            super(itemView);
            if (b) {
                tv_user_comment_detele = (TextView) itemView.findViewById(R.id.tv_user_comment_detele);
                iv_user_photo = (CircleImageView) itemView
                        .findViewById(R.id.iv_user_photo);
                tv_user_name = (TextView) itemView
                        .findViewById(R.id.tv_user_name);
                tv_user_comment = (TextView) itemView
                        .findViewById(R.id.tv_user_comment);
                btn_comment_reply = (TextView) itemView
                        .findViewById(R.id.tv_user_reply);
                lv_user_comment_replys = (MyListView) itemView
                        .findViewById(R.id.lv_user_comment_replys);
                tv_user_time = (TextView) itemView.findViewById(R.id.tv_user_tiem);
                tv_user_praise = (TextView) itemView.findViewById(R.id.tv_user_praise);
                commit_linear = (LinearLayout) itemView.findViewById(R.id.commit_linear);
                lv_user_comment_replys2 = (MyListView) itemView.findViewById(R.id.lv_user_comment_replys2);
            }

        }
    }


    /**
     * 底部
     */
    public class FootAdaterViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout footer_liner;
        private ProgressBar progressBar;
        private TextView footer_text;

        public FootAdaterViewHolder(View itemView) {
            super(itemView);
            progressBar = (ProgressBar) itemView.findViewById(R.id.footer_progressbar);
            footer_liner = (LinearLayout) itemView.findViewById(R.id.footer_liner);
            footer_text = (TextView) itemView.findViewById(R.id.footer_text);

        }
    }


    public void addDataa(Comment comment, int position) {
        commentList.add(position, comment);
        notifyItemInserted(position);
        notifyDataSetChanged();

    }

    public void clearList() {
        this.commentList.clear();
    }

    public void updateList(List<Comment> commentList) {
        this.commentList.addAll(commentList);


    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    /**
     * 添加数据
     */
    public void addDatas(List<Comment> addLists) {//判断上拉0还是下拉1
        if (addLists != null) {

            commentList.addAll(addLists);
            notifyDataSetChanged();
        }
    }

    /**
     * 添加一个数据
     *
     * @param t
     * @param isclear
     */
    public void appendData(Comment t, int position) {
        commentList.add(position, t);
        notifyItemInserted(position);
//        notifyItemRangeChanged(position, commentList.size() - position);
    }

    public void addData(int position, Comment comment) {

        commentList.add(position, comment);
        notifyItemInserted(position);
        notifyItemChanged(position); //更新
//        notifyItemRangeChanged(position, commentList.size() - position);
    }

    /**
     * 移除数据
     *
     * @param position
     */
    public void removeData(int position) {
        commentList.remove(position);
        notifyItemRemoved(position);
//        notifyItemRangeChanged(position, commentList.size() - position);
    }

    /**
     * 设置上拉状态
     *
     * @param status
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }

    private LoadMoreDataListener mMoreDataListener;

    //加载更多监听方法
    public void setOnMoreDataLoadListener(LoadMoreDataListener onMoreDataLoadListener) {
        mMoreDataListener = onMoreDataLoadListener;
    }

    public void setLoaded() {
        isLoading = false;
    }
}
